package com.besall.allbase.view.activity.chipstoollevel4.protocol.FitnessParams;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;
import com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants;

public class DailyWalkTarget extends DialogFragment {

    public EditText mSerial;
    private ConfigCallBack mConfigCallback;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.config_dailywalktarget, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){
        mSerial = (EditText) view.findViewById(R.id.serial);
        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);

    }

    public boolean saveConfig(){
        String serial = mSerial.getText().toString();
        if (TextUtils.isEmpty(serial)) {
            Toast.makeText(getActivity(), "can not be empty", Toast.LENGTH_SHORT).show();
            return false;
        }
        SPHelper.putPreference(getActivity(), ProtocolConstants.KEY_DAILY_WALK_TARGET_VALUE, Integer.valueOf(serial));
        return true;
    }

    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }

    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };


    public interface ConfigCallBack {

        void onConfigOk();

        void onConfigCancel();
    }



}
